<?php
function kw_site_name($print = true) {
  global $kw_config;
  $site_name = htmlspecialchars($kw_config['site_name']);
  if ($print) {
    echo $site_name;
    return;
  }
  return $site_name;
}

function kw_site_desc($print = true) {
  global $kw_config;
  $site_desc = htmlspecialchars($kw_config['site_desc']);
  if ($print) {
    echo $site_desc;
    return;
  }
  return $site_desc;
}

function kw_site_link($print = true) {
  global $kw_config;
  $site_link = $kw_config['site_link'];
  if ($print) {
    echo $site_link;
    return;
  }
  return $site_link;
}

function kw_nick_name($print = true) {
  global $kw_config;
  $nick_name = htmlspecialchars($kw_config['user_nick']);
  if ($print) {
    echo $nick_name;
    return;
  }
  return $nick_name;
}

function kw_theme_url($path, $print = true) {
  global $kw_config;
  $url = $kw_config['site_link'].'/kw_users/theme/v/'.$path;
  if ($print) {
    echo $url;
    return;
  }
  return $url;
}

function kw_is_post() {
  global $kw_get_type;
  return $kw_get_type == 'post';
}

function kw_is_page() {
  global $kw_get_type;
  return $kw_get_type == 'page';
}

function kw_is_tag() {
  global $kw_get_type;
  return $kw_get_type == 'tag';
}

function kw_is_date() {
  global $kw_get_type;
  return $kw_get_type == 'date';
}

function kw_is_archive() {
  global $kw_get_type;
  return $kw_get_type == 'archive';
}

function kw_tag_name($print=true) {
  global $kw_get_name;
  if ($print) {
    echo htmlspecialchars($kw_get_name);
    return;
  }
  return $kw_get_name;
}

function kw_date_name($print=true) {
  global $kw_get_name;
  if ($print) {
    echo htmlspecialchars($kw_get_name);
    return;
  }
  return $kw_get_name;
}

function kw_has_new() {
  global $kw_page_num;
  return $kw_page_num != 1;
}

function kw_has_old() {
  global $kw_page_num, $kw_post_count, $kw_post_per_page;
  return $kw_page_num < ($kw_post_count / $kw_post_per_page);
}

function kw_goto_old($text) {
  global $kw_get_type, $kw_get_name, $kw_page_num, $kw_config;
  if ($kw_get_type == 'tag') {
    echo '<a href="';
    echo $kw_config['site_link'];
    echo '/?tag/';
    echo $kw_get_name;
    echo '/?page=';
    echo ($kw_page_num + 1);
    echo '" class="nextpage">';
    echo $text;
    echo '</a>';
  } 
  elseif ($kw_get_type == 'date') {
    echo '<a href="';
    echo $kw_config['site_link'];
    echo '/?date/';
    echo $kw_get_name;
    echo '/?page=';
    echo ($kw_page_num + 1);
    echo '" class="nextpage">';
    echo $text;
    echo '</a>';
  } else {
    echo '<a href="';
    echo $kw_config['site_link'];
    echo '/?page=';
    echo ($kw_page_num + 1);
    echo '" class="nextpage">';
    echo $text;
    echo '</a>';
  }
}

function kw_goto_new($text) {
  global $kw_get_type, $kw_get_name, $kw_page_num, $kw_config;
  if ($kw_get_type == 'tag') {
    echo '<a href="';
    echo $kw_config['site_link'];
    echo '/?tag/';
    echo $kw_get_name;
    echo '/?page=';
    echo ($kw_page_num - 1);
    echo '" class="prevpage">';
    echo $text;
    echo '</a>';
  }
  elseif ($kw_get_type == 'date') {
    echo '<a href="';
    echo $kw_config['site_link'];
    echo '/?date/';
    echo $kw_get_name;
    echo '/?page=';
    echo ($kw_page_num - 1);
    echo '" class="prevpage">';
    echo $text;
    echo '</a>';
  } else {
    echo '<a href="';
    echo $kw_config['site_link'];
    echo '/?page=';
    echo ($kw_page_num - 1);
    echo '" class="prevpage">';
    echo $text;
    echo '</a>';
  }
}

function kw_date_list($item_begin='<li>', $item_gap='', $item_end='</li>') {
  global $kw_dates, $kw_config;
  if (isset($kw_dates)) {
    $date_count = count($kw_dates);
    for ($i = 0; $i < $date_count; $i ++) {
      $date = $kw_dates[$i];
      echo $item_begin;
      echo '<a href="';
      echo $kw_config['site_link'];
      echo '/?date/';
      echo $date;
      echo '/">';
      echo $date;
      echo '</a>';
      echo $item_end;
      if ($i < $date_count - 1)
        echo $item_gap;
    }
  }
}

function kw_tag_list($item_begin='<li>', $item_gap='', $item_end='</li>') {
  global $kw_tags, $kw_config;
  if (isset($kw_tags)) {
    $tag_count = count($kw_tags);
    for ($i = 0; $i < $tag_count; $i ++) {
      $tag = $kw_tags[$i];
      echo $item_begin;
      echo '<a href="';
      echo $kw_config['site_link'];
      echo '/?tag/';
      echo urlencode($tag);
      echo '/">';
      echo $tag;
      echo '</a>';
      echo $item_end;
      if ($i < $tag_count - 1)
        echo $item_gap;
    }
  }
}

function kw_next_post() {
  global $kw_posts, $kw_post_ids, $kw_post_count, $kw_post_i, $kw_post_i_end, $kw_post_id, $kw_post, $kw_page_num, $kw_post_per_page;
  if (!isset($kw_posts))
    return false;
  if (!isset($kw_post_i)) {
    $kw_post_i = 0 + ($kw_page_num - 1) * $kw_post_per_page;
    $kw_post_i_end = $kw_post_i + $kw_post_per_page;
    if ($kw_post_count < $kw_post_i_end)
      $kw_post_i_end = $kw_post_count;
  }
  if ($kw_post_i == $kw_post_i_end)
    return false;
  $kw_post_id = $kw_post_ids[$kw_post_i];
  $kw_post = $kw_posts[$kw_post_id];
  $kw_post_i += 1;
  return true;
}

function kw_the_title($print = true) {
  global $kw_post;
  if ($print) {
    echo htmlspecialchars($kw_post['title']);
    return;
  }
  return htmlspecialchars($kw_post['title']);
}

function kw_the_date($print = true) {
  global $kw_post;
  if ($print) {
    echo $kw_post['date'];
    return;
  }
  return $kw_post['date'];
}

function kw_the_time($print = true) {
  global $kw_post;
  if ($print) {
    echo $kw_post['time'];
    return;
  }
  return $kw_post['time'];
}

function kw_the_tags($item_begin='', $item_gap=', ', $item_end='') {
  global $kw_post, $kw_config;
  $tags = $kw_post['tags'];
  $count = count($tags);
  for ($i = 0; $i < $count; $i ++) {
    $tag = htmlspecialchars($tags[$i]);
    echo $item_begin;
    echo '<a href="';
    echo $kw_config['site_link'];
    echo '/?tag/';
    echo urlencode($tag);
    echo '/">';
    echo $tag;
    echo '</a>';
    echo $item_end;
    if ($i < $count - 1)
      echo $item_gap;
  }
}

function kw_the_content($print = true) {
  global $kw_data, $kw_config;
  if (!isset($kw_data)) {
    global $kw_post_id;
      // TODO LITONG
    $data = unserialize(file_get_contents('files/posts/data/'.$kw_post_id.'.dat')); 
    $html = $data['content']; 
  }
  else {
    $html = $kw_data['content'];
  }
  $wwww = substr($kw_config['site_link'],5);
  if ($print) {
    echo str_replace('//BASE_HTTP_HOST/',$wwww,$html);
    return;
  }
  return str_replace('//BASE_HTTP_HOST/',$wwww,$html);
}

function kw_the_link() {
  global $kw_post_id, $kw_post, $kw_config;
  echo '<a href="';
  echo $kw_config['site_link'];
  echo '/?post/';
  echo $kw_post_id;
  echo '">';
  echo htmlspecialchars($kw_post['title']);
  echo '</a>';
}

function kw_post_link() {
  global $kw_post_id, $kw_post, $kw_config;
  echo $kw_config['site_link'];
  echo '/?post/';
  echo $kw_post_id;
}

function kw_page_title() {
  global $kw_post_id, $kw_post, $kw_config;
  echo htmlspecialchars($kw_post['title']);
}

function kw_can_comment() {
  global $kw_post_id, $kw_post;
  return isset($kw_post['can_comment']) ? $kw_post['can_comment'] == '1' : true;
}

function kw_comment_code() {
  global $kw_config;
  echo isset($kw_config['comment_code']) ? $kw_config['comment_code'] : '';
}
?>
